from flask import Flask, request
import socketio

app = Flask(__name__)
app.mgr = socketio.KombuManager('amqp://python:rabbitmqpwd@localhost:5672/toutiao')

@app.route('/')
# def index():
#     sid = request.args.get('sid')
#     msg = request.args.get('msg')
#     if sid and msg:
#         # sid和msg都存在,才发送
#         app.mgr.emit('message', msg, room=sid)
#     return  'index'

@app.route('/')
def index():
    user_id = request.args.get('user_id')
    msg = request.args.get('msg')
    if user_id and msg:
        # user_id和msg都存在,才发送
        # user_id此时是一个房间名称
        # 这里的意思是往user_id这个房间群发消息
        app.mgr.emit('message', msg, room = user_id)
    return 'index'

if __name__ == '__main__':
    app.run(host='192.168.226.129', debug=True)